package com.caocaog.market.applets;

import com.caocaog.market.annotation.IgnoreAuth;
import com.caocaog.market.entity.GoodsBase;
import com.caocaog.market.entity.GoodsCategories;
import com.caocaog.market.entity.ShopBasic;
import com.caocaog.market.entity.vo.AllGoodsVO;
import com.caocaog.market.entity.vo.GoodsVO;
import com.caocaog.market.service.GoodsCategoriesService;
import com.caocaog.market.service.GoodsCleanCacheService;
import com.caocaog.market.service.GoodsService;
import com.caocaog.market.service.IShopBasicService;
import com.caocaog.market.util.CollectionsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小程序商品信息
 *
 * @author chechen
 * @date 2022-04-14
 */
@Slf4j
@RestController
@Api(value = "goods", tags = "小程序商品信息")
@RequestMapping("/api/goods")
public class GoodsController {

    private GoodsService goodsService;
    private GoodsCleanCacheService goodsCleanCacheService;
    private GoodsCategoriesService goodsCategoriesService;
    @Resource
    private IShopBasicService shopBasicService;

    @IgnoreAuth
    @ApiOperation(value = "小程序进入店铺时，获取所有商品信息")
    @GetMapping
    public Result<List<AllGoodsVO>> getAllGoodsInfoByShopId(@RequestParam String shopId) {
        log.info("小程序进入店铺时，获取所有商品信息");
        return Result.OK(this.goodsService.getAllGoodsInfo(shopId));
    }


    /**
     * 随机推荐一个店铺的商品信息
     *
     * @return 随机店铺商品信息
     */
    @IgnoreAuth
    @ApiOperation(value = "获取商品推荐，随机查一个店铺的商品信息")
    @GetMapping("/randomGoods")
    public Result<List<GoodsVO>> getAllGoodsInfoByRandomShop() {
        // 查询单商户下的所有店铺
        List<ShopBasic> shopList = shopBasicService.getOpenShopByMerchantId("110");
        if (CollectionsUtil.isEmpty(shopList)) {
            return Result.error("数据为空！", null);
        }
        // 随机挑选一个数据
        ShopBasic shopBasic = CollectionsUtil.pickRandomElement(shopList);
        String indexShopId = shopBasic.getId();
        log.info("小程序进入店铺时，获取随机店铺id:{}商品信息", indexShopId);
        return Result.OK(this.goodsService.getAllGoodsInfoByShopId(indexShopId));
    }

    /**
     * 热推商品信息
     *
     * @return 热推商品信息
     */
    @IgnoreAuth
    @ApiOperation(value = "获取热销商品")
    @GetMapping("/hotGoods")
    public Result<List<GoodsVO>> getHotGoodsInfoByRandomShop() {
        // 查询单商户下的所有店铺
        List<ShopBasic> shopList = shopBasicService.getOpenShopByMerchantId("110");
        if (CollectionsUtil.isEmpty(shopList)) {
            return Result.error("数据为空！", null);
        }
        // 随机挑选一个数据
        ShopBasic shopBasic = CollectionsUtil.pickRandomElement(shopList);
        String indexShopId = shopBasic.getId();
        log.info("小程序进入店铺时，获取随机店铺id:{}商品信息", indexShopId);
        return Result.OK(this.goodsService.getHotGoodsInfoByShopId(indexShopId));
    }

    /**
     * 首页商品信息
     *
     * @return 首页商品信息
     */
    @IgnoreAuth
    @ApiOperation(value = "获取商品推荐")
    @GetMapping("/indexGoods")
    public Result<List<GoodsVO>> getIndexGoodsInfoByRandomShop() {
        // 查询单商户下的所有店铺
        List<ShopBasic> shopList = shopBasicService.getOpenShopByMerchantId("110");
        if (CollectionsUtil.isEmpty(shopList)) {
            return Result.error("数据为空！", null);
        }
        // 随机挑选一个数据
        ShopBasic shopBasic = CollectionsUtil.pickRandomElement(shopList);
        String indexShopId = shopBasic.getId();
        log.info("小程序进入店铺时，获取随机店铺id:{}商品信息", indexShopId);
        return Result.OK(this.goodsService.getIndexGoodsInfoByShopId(indexShopId));
    }

    /**
     * 根据频道取获取商品信息，走缓存
     * @param channelId 频道Id
     * @return 查询到的商品信息
     */
    @IgnoreAuth
    @ApiOperation("根据频道Id获取商品信息")
    @GetMapping("/getGoodsByChannelId")
    public Result<?> getGoodsByChannelId(@RequestParam String channelId) {
        List<GoodsBase> goodList = goodsService.getAllGoodsByChannel(channelId);
        return Result.OK("查询成功！",goodList);
    }


    @IgnoreAuth
    @ApiOperation(value = "小程序进入店铺时，获取首个商品规格内的商品信息(不满20个继续请求下一个分类)")
    @GetMapping("/first")
    public Result<List<AllGoodsVO>> getFirstCategoriesGoodsInfo(@RequestParam String shopId) {
        log.info("小程序进入店铺时，获取所有商品信息");
        return Result.OK(this.goodsService.getFirstCategoriesGoodsInfo(shopId));
    }


    @IgnoreAuth
    @ApiOperation(value = "根据店铺id 删除关于店铺内所有商品的缓存")
    @DeleteMapping("/cleanCacheGoodsAll")
    public Result<Object> cleanCacheGoodsAll(@RequestParam String shopId) {
        this.goodsCleanCacheService.cleanCacheGoodsAll(shopId);
        return Result.OK();
    }

    @IgnoreAuth
    @ApiOperation(value = "根据店铺id 删除 商品信息集合 缓存")
    @DeleteMapping("/cleanCacheGoodsInfoByShopId")
    public Result<Object> cleanCacheGoodsInfoByShopId(@RequestParam String shopId) {
        this.goodsCleanCacheService.cleanCacheGoodsInfoByShopId(shopId);
        return Result.OK();
    }

    @IgnoreAuth
    @ApiOperation(value = "根据商品分类id 删除 商品信息集合 缓存")
    @DeleteMapping("/cleanCacheGoodsInfoByCategoriesId")
    public Result<Object> cleanCacheGoodsInfoByCategoriesId(@RequestParam String categoriesId) {
        this.goodsCleanCacheService.cleanCacheGoodsInfoByCategoriesId(categoriesId);
        return Result.OK();
    }

    @IgnoreAuth
    @ApiOperation(value = "根据店铺id 删除 商品分类 缓存")
    @DeleteMapping("/cleanCacheGoodsCategories")
    public Result<Object> cleanCacheGoodsCategories(@RequestParam String shopId) {
        this.goodsCleanCacheService.cleanCacheGoodsCategories(shopId);
        return Result.OK();
    }

    @IgnoreAuth
    @ApiOperation(value = "根据店铺id 删除 商品基础信息 缓存")
    @DeleteMapping("/cleanCacheGoodsBase")
    public Result<Object> cleanCacheGoodsBase(@RequestParam String shopId) {
        this.goodsCleanCacheService.cleanCacheGoodsBase(shopId);
        return Result.OK();
    }

    @IgnoreAuth
    @ApiOperation(value = "根据店铺id 删除 商品规格 缓存")
    @DeleteMapping("/cleanCacheGoodsSpec")
    public Result<Object> cleanCacheGoodsSpec(@RequestParam String shopId) {
        this.goodsCleanCacheService.cleanCacheGoodsSpec(shopId);
        return Result.OK();
    }

    @IgnoreAuth
    @ApiOperation(value = "根据店铺id 删除 商品属性 缓存")
    @DeleteMapping("/cleanCacheGoodsAttr")
    public Result<Object> cleanCacheGoodsAttr(@RequestParam String shopId) {
        this.goodsCleanCacheService.cleanCacheGoodsAttr(shopId);
        return Result.OK();
    }

    @IgnoreAuth
    @ApiOperation(value = "店铺商品分类菜单")
    @PostMapping("/getShopSortList")
    public Result<List<GoodsCategories>> getShopSortList(@RequestParam String shopId) {
        return Result.OK(this.goodsCategoriesService.getShowGoodsCategoriesByShopId(shopId));
    }


    @Autowired
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @Autowired
    public void setGoodsCleanCacheService(GoodsCleanCacheService goodsCleanCacheService) {
        this.goodsCleanCacheService = goodsCleanCacheService;
    }

    @Autowired
    public void setGoodsCategoriesService(GoodsCategoriesService goodsCategoriesService) {
        this.goodsCategoriesService = goodsCategoriesService;
    }

}
